import { HardhatRuntimeEnvironment } from "hardhat/types";
import { DeployFunction } from "hardhat-deploy/types";
import {ethers, upgrades} from "hardhat";
import { SignerWithAddress } from "@nomiclabs/hardhat-ethers/dist/src/signer-with-address";
import {write} from "../../utils/io";
import {
  BlindBoxToken,
  BlindBoxToken__factory, EuroCup, EuroCup__factory,
  TermToken, TermToken__factory,
  VoucherToken, VoucherToken__factory
} from "../../types";
import {USDB_ADDRESS,BLAST_ENTROPY_ADDRESS} from "../../utils/constants";
const SALE_START_BLOCK = 3000000; //需要修改
const PLAY_START_BLOCK = 3000000; //需要改为4790000
const SALE_FINISH_BLOCK = 5222000;
const PUBLISH_START_BLOCK = 3000000; //需要改为6100000
const PLAY_FINISH_BLOCK = 6260000;

const func: DeployFunction = async (hre: HardhatRuntimeEnvironment) => {
  const filePath = hre.config.paths.deployments+"\\"+hre.network.name;
  await write(filePath,".chainId",await hre.getChainId());
  let signer: SignerWithAddress;
  [signer] = await ethers.getSigners();
  console.log("-----singer address=",signer.address);
  let vToken: VoucherToken;
  let tToken: TermToken;
  let bToken: BlindBoxToken;
  let euroCup: EuroCup;
  let MINTER_ROLE = ethers.utils.keccak256(ethers.utils.toUtf8Bytes("MINTER_ROLE"));
  let BURNER_ROLE = ethers.utils.keccak256(ethers.utils.toUtf8Bytes("BURNER_ROLE"));
  //发布VoucherToken合约
  const voucherTokenFactory = new VoucherToken__factory(signer);
  vToken = await voucherTokenFactory.deploy();
  await vToken.deployed();
  console.log("-----vToken address=",vToken.address);
  await write(filePath,"VoucherToken.json",JSON.stringify({"address":vToken.address,"abi":VoucherToken__factory.abi}));
  //发布TermToken合约
  const termTokenFactory = new TermToken__factory(signer);
  tToken = await termTokenFactory.deploy("");
  await tToken.deployed();
  console.log("-----tToken address=",tToken.address);
  await write(filePath,"TermToken.json",JSON.stringify({"address":tToken.address,"abi":TermToken__factory.abi}));
  //发布BlindBoxToken合约
  const blindBoxTokenFactory = new BlindBoxToken__factory(signer);
  bToken = await blindBoxTokenFactory.deploy();
  await bToken.deployed();
  console.log("-----bToken address=",bToken.address);
  await write(filePath,"BlindBoxToken.json",JSON.stringify({"address":bToken.address,"abi":BlindBoxToken__factory.abi}));
  //发布EuroCup合约
  const euroCupFactory = new EuroCup__factory(signer);
  //需要修改几个时间使其符合测试条件
  euroCup = await upgrades.deployProxy(euroCupFactory,[tToken.address,vToken.address,bToken.address,USDB_ADDRESS,SALE_START_BLOCK,PLAY_START_BLOCK,SALE_FINISH_BLOCK,PUBLISH_START_BLOCK,PLAY_FINISH_BLOCK,BLAST_ENTROPY_ADDRESS]) as EuroCup;
  await euroCup.deployed();
  console.log("-----euroCup address=",euroCup.address);
  await write(filePath,"EuroCup.json",JSON.stringify({"address":euroCup.address,"abi":EuroCup__factory.abi}));
  //grantRole
  await tToken.grantRole(MINTER_ROLE,euroCup.address);
  await tToken.grantRole(BURNER_ROLE,euroCup.address);
  await vToken.grantRole(MINTER_ROLE,euroCup.address);
  await vToken.grantRole(BURNER_ROLE,euroCup.address);
  await bToken.grantRole(MINTER_ROLE,euroCup.address);
  await bToken.grantRole(BURNER_ROLE,euroCup.address);
};

func.tags = ["TokenDeploy"];

export default func;
